package com.tgpms.web.excelTable.service.impl;

import com.tgpms.web.excelTable.dao.ExcelTableColumnMapper;
import com.tgpms.web.excelTable.entity.ExcelTableColumn;
import com.tgpms.web.excelTable.service.IExcelTableColumnService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.UUID;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;

/**
 * <p>
 * 动态表格模板列表 服务实现类
 * </p>
 *
 * @author newway
 * @since 2020-01-17
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class ExcelTableColumnServiceImpl implements IExcelTableColumnService {

	@Resource
	private ExcelTableColumnMapper mapper;
	
	/**
		查询动态表格模板列表树形
	 */
	@Override
	public Result selExcelTableColumnTree(String tcId, String ttId,Result result) {
		QueryWrapper<ExcelTableColumn> wrapper = new QueryWrapper<ExcelTableColumn>();
		if(!Common.isEmpty(tcId)) {
			wrapper.eq("PARENT_ID", tcId);
		}else {
			wrapper.eq("PARENT_ID", "0");
		}
		if(!Common.isEmpty(ttId)) {
			wrapper.eq("TT_ID",ttId);
		}
		wrapper.orderByAsc("ORDER_NO");
		List<Object> list = new ArrayList<Object>();
		List<ExcelTableColumn> onelist = mapper.selectList(wrapper);
		if(onelist!=null && onelist.size()>0) {
			for(ExcelTableColumn en : onelist) {
				JSONObject tempJson = (JSONObject) JSONObject.toJSON(en);
				tempJson.remove("insertUser");
                tempJson.remove("insertDate");
                tempJson.remove("modifyUser");
                tempJson.remove("modifyDate");
                tempJson.remove("rowstamp");
                List<Object> children = selExcelTableColumnChildren(en.getTcId());
                tempJson.put("children", children);
                
                list.add(tempJson);
			}
		}
		result.setMsg(ExceptionExplain.SUCCESS_BY_INFOS.getExplain());
		result.setData(list);
		return result;
	}
	
	private List<Object> selExcelTableColumnChildren(String tcId) {
		QueryWrapper<ExcelTableColumn> wrapper = new QueryWrapper<ExcelTableColumn>();
		List<Object> list = new ArrayList<Object>();
		wrapper.eq("parent_id", tcId);
		wrapper.orderByAsc("orderNo");
		List<ExcelTableColumn> children = mapper.selectList(wrapper);
		if(children!=null && children.size()>0) {
			for(ExcelTableColumn en : children) {
				JSONObject tempJson = (JSONObject) JSONObject.toJSON(en);
				tempJson.remove("insertUser");
                tempJson.remove("insertDate");
                tempJson.remove("modifyUser");
                tempJson.remove("modifyDate");
                tempJson.remove("rowstamp");
                tempJson.put("children", selExcelTableColumnChildren(en.getTcId()));
                
                list.add(tempJson);
			}
		}
		return list;
	}
	
	/**
		新增动态表格模板列表
	*/
	@Override
	public Result addExcelTableColumn(ExcelTableColumn en,Result result) {
		String id = Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
		en.setTcId(id);
		en.setRowstamp(id);
		en.setInsertDate(new Date());
		int i = mapper.insert(en);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(id);
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	/**
		更新动态表格模板列表
	*/
	@Override
	public Result updateExcelTableColumn(ExcelTableColumn en,Result result) {
		int i = mapper.updateById(en);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	/**
		根据主键删除动态表格模板列表
	*/
	@Override
	public Result deleteExcelTableColumn(Serializable id,Result result) {
		int i = mapper.deleteById(id);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

}
